<?php
/**
 * 
 * 用户打标签的微博
 * @author yangchao
 * @since 2012-03-19
 */
if (!file_exists("../SINASRV_CONFIG")) {
	echo "Read the sinasrv config error!\n";
//	exit();
} else {
   $array_config = parse_ini_file("../SINASRV_CONFIG", true); var_dump($array_config);
   
   $_SERVER = array_merge($_SERVER, $array_config); 
}


/*ini_set("display_errors",ture);
error_reporting(E_ALL);*/

define("CRON_NAME","cron_get_by_biaoqian");//cro名称，要求唯一
set_time_limit(0);
$_GET["db_debug"] = true;
$_GET["no_pref_debug"] = true;
$_GET["api_debug"] = true;

//15分钟一次,执行1000条
define("PCOUNT",10);//每次处理多少mid
define("MAX_PAGE",1000); //定义每个进程处理最大的页数
define("TOTAL_PROCESS",1);//添加新进程需要更新这个配置
define("NOW_PROCESS",$argv[1] ? $argv[1] : 0);//当前进程ID=0 可以申请如下php xxx 1
define("CRON_START_TIME",time());



include_once($_SERVER['DOCUMENT_ROOT'] ."/include/user/inc.php");
include_once($_SERVER['DOCUMENT_ROOT'] ."/include/tucao/inc.php");
include_once($_SERVER['DOCUMENT_ROOT'] ."/include/mblog/inc.php");
include_once($_SERVER['DOCUMENT_ROOT'] ."/include/album/inc.php");

class mod_cron_engine_main extends mod_cron_engine
{
	function __construct($current_process_id)
	{
		parent::__construct($current_process_id,CRON_NAME);
	}
	
	
	function craw()
	{
		$this->get_data();
	}
	
	
	function  run()
	{
		//开始跑cron之前预先获取数据（start_id等）
		$this->before_run();
		
		//正式获取数据
		$this->craw();
		
		
		$this->after_run();
	}
	
	
	function get_data()
	{
		
		$db_mblog_cron_topic_tag = new db_mblog_cron_topic_tag();
		$mblog_from_api = new mblog_from_api();
		
		$db_album_info = new db_album_info();		
		
		//获取tag的总数
		$count = $db_album_info->cron_get_album_count_is_system($this->start_id);
		
		if($count == 0)
		{
			//如果获取不到需要更新的数据，初始化cron_start_id
			$this->ini_start_id(NOW_PROCESS);
			$this->start_id = 0 ;
			$count = $db_album_info->cron_get_album_count_is_system($this->start_id);
		}
		
		
		
		echo "tag_count:$count<br>\n";
		$total_page = ceil($count/PCOUNT);
		if($total_page > MAX_PAGE && defined("MAX_PAGE")) $total_page = MAX_PAGE;
			
		for($i=1;$i<=$total_page;$i++)
		{
			$this->get_list_by_page($i);
		}
		
		
	}	
	
	function get_list_by_page($p)
	{
		$db_album_info = new db_album_info();
		$db_mblog_cron_topic_tag = new db_mblog_cron_topic_tag();
		//分批次取tag名称
		$food_tag = $db_album_info->cron_get_album_list_is_system($p,PCOUNT,$this->start_id);
		
		foreach($food_tag as $value)
		{
			$create_uid = $value["create_uid"];
			$id = $value["id"];
			$mblog_topic  = $value["mblog_topic"];
			$mblog_topic_id  = $value["mblog_topic_id"];
			
			if($id % TOTAL_PROCESS != NOW_PROCESS)//用总进程做控制 example: 3个进程  php xxx 1 只会执行除3余1的id
			{
				continue;
			}
			
			$show_type = $value["show_type"];
			if($mblog_topic_id)//处理抓取分页逻辑
			{
				$topic_info = $db_mblog_cron_topic_tag->get_mblog_cron_topic_tag_item_by_id($mblog_topic_id);
				$this->get_list_by_topic(1,$create_uid,$topic_info["topic"],$mblog_topic_id);
				if($topic_info["topic_alias"])$this->get_list_by_topic(1,$create_uid,$topic_info["topic"],$mblog_topic_id,$topic_info["topic_alias"]);
				
			}
			
			if($id)//更新抓取的start_id到数据库
			{
				$done_job = true;
				$res = $this->replace_into_data($id);	
			}
			
			
		}
		
		if($done_job == false)
		{
			$this->ini_start_id();
		}
			
	}
	
	
	//根据话题抓取数据并入库
	function get_list_by_topic($p=1,$uid,$topic,$id,$topic_alias="")
	{
		global $config;
		$max_time = "";
		//执行时间超过30分钟，退出
		if(time() - CRON_START_TIME > 3600)
		{
			exit;
		}
		$db_base = new db_base();
		$mblog_from_api = new mblog_from_api();
		$db_user_search = new  db_user_search();
		$db_user_info = new  db_user_info();
		$db_mblog_cron_topic_tag = new  db_mblog_cron_topic_tag();
		
		
		$db_mblog_pic_content = new db_mblog_pic_content();
		if($topic)
		{
			
			if(!$starttime)
			{
				$starttime = strtotime($starttime["create_time"]);
				
			}
			
			//根据话题抓取
			if($topic_alias)
			{
				$mblog_list = $mblog_from_api->api_get_tag_timeline($topic_alias,$uid,0,0,20,$p);
			}
			else
			{
				$mblog_list = $mblog_from_api->api_get_tag_timeline($topic,$uid,0,0,20,$p);
			}
			
			$mids = $mblog_list["data"]["result"]["statuses"];
			$count =  $mblog_list["data"]["result"]["total_number"]; 
			
			if(!$mids)
			{
				return false;
			}
			
			$mids = join(",",$mids);
			if(!$mids)
			{
				return false;
			}
			$mblog_list = $mblog_from_api->api_get_mblog_by_mids($mids,$trim_user=0);
			
			if(!$mblog_list["data"]["result"])
			{
				return false;
			}
		
			if($mblog_list["data"]["result"])
			{
					
				foreach($mblog_list["data"]["result"] as $value1)
				{
					$mid = $value1["mid"] ;
						
					$u_time = strtotime($value1["created_at"]);
					if($max_time < $u_time) $max_time = $u_time;
					$create_time = date("Y-m-d H:i:s",$u_time);
					
					
					if(!$value1["bmiddle_pic"])//如果有这个字段，肯定是原创并且带图
					{
						return false;
					}
					
					
					$ary["uid"] = $uid;
					$ary["mid"] = $mid;
					$inert_res = $db_base->insert("mblog_cron_biaoqian",$ary);
					var_dump($inert_res);
					if(!$inert_res)//数据库不需要重复抓取，图片抓取慢，这里优先判断
					{
						return false;
					}
					
					$get_count = $db_mblog_pic_content->cron_get_count_by_mid($mid);
					
					var_dump($get_count);
					if($get_count > 0)
					{
						return false;//已经有数据
					}
					
					
					
					$topic_info = $db_mblog_cron_topic_tag->get_mblog_cron_topic_tag_item_by_id($id);
					$show_type = $topic_info["show_type"];
					$res = get_image::get_image_size_by_url($value1["bmiddle_pic"]);
					
					$srcW = $res["width"]; 
					$srcH = $res["height"]; 
					
					
					echo "image:{$value1["bmiddle_pic"]} $srcW - $srcH \n";
					
					
					
					$pic_width_pubu = PUBU_WIDTH;
					$pic_height_pubu = intval($srcH/($srcW/$pic_width_pubu));
					$value1["text"] = string::un_html($value1["text"]);
					$value1["text"] = mysql_escape_string($value1["text"]);
					$date =  date("Y-m-d",strtotime($value1["created_at"]));
					$reposts_count = $value1["reposts_count"] ;
					$last_insert_id = $db_mblog_pic_content->cron_biaoqian_insert_mblog_pic_content($value1["text"],$id,$topic,$value1["bmiddle_pic"],$pic_width_pubu,$pic_height_pubu,$value1["source"],$show_type,$from=101,$value1["user"]["id"],$value1["user"]["screen_name"],$date,$mid,$create_time,$reposts_count);
					
					if($last_insert_id)
					{
						//如果插入成功，更新用户搜索表和用户表的微博个数
						$db_user_search->update_user_search_by_uid($value1["user"]["id"],$id,1);
						$db_user_info->update_user_count_by_uid($value1["user"]["id"],"","",1);
						
					}
					
					
				}
				
			}
			
			if($p == 1)
			{
				$total_page = ceil($count/20);
				for($i= 2;$i <=$total_page ;$i++ )
				{
					$this->get_list_by_topic($i,$uid,$topic,$id,$topic_alias);
				}
				
			}
			
			
			////处理end
			
			
		}
		
			
	}
	
}

$m = new mod_cron_engine_main(NOW_PROCESS);
$m->run();
